package com.videoaggre.zcl.ui.widget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.toColor
import com.videoaggre.zcl.common.visible
import kotlinx.android.synthetic.main.title_bar_layout.view.*

class TitleBarLayout : FrameLayout, View.OnClickListener {
    companion object {
        const val TBL_MAIN = 1 // 主色调
        const val TBL_LOGIN = 2 // 登录
    }

    private var mTblStyle: Int = TBL_MAIN

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        LayoutInflater.from(context).inflate(R.layout.title_bar_layout, this)
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout)
            mTblStyle = typedArray.getInt(R.styleable.TitleBarLayout_tbl_style, mTblStyle)
            rightImage = typedArray.getResourceId(R.styleable.TitleBarLayout_right_image, 0)
            rightStr = typedArray.getString(R.styleable.TitleBarLayout_right_str) ?: ""
            rightTextColor = typedArray.getColor(R.styleable.TitleBarLayout_right_str_color, R.color.white.toColor())
            titleStr = typedArray.getString(R.styleable.TitleBarLayout_title_str) ?: ""
            isTitleBold = typedArray.getBoolean(R.styleable.TitleBarLayout_is_title_bold, false)
            isShowLine = typedArray.getBoolean(R.styleable.TitleBarLayout_is_show_line, true)
            isShowLeft = typedArray.getBoolean(R.styleable.TitleBarLayout_is_show_left, true)
            typedArray.recycle()
        }
        changeStyle(mTblStyle)
        ivBack.setOnClickListener(this)
        ivRightImage.setOnClickListener(this)
        tvRightText.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v) {
            ivBack -> {
                onLeftClickListener?.invoke(v)
                if (context is Activity) (context as Activity).onBackPressed()
            }
            ivRightImage -> onRightImageClickListener?.invoke(v)
            tvRightText -> onRightTextClickListener?.invoke(v)
        }
    }

    fun changeStyle(tblStyle: Int) {
        when (tblStyle) {
            TBL_MAIN -> {
                leftImage = R.mipmap.nav_btn_return
                titleTextColor = R.color.white.toColor()
                bgColor = ColorDrawable(R.color.main_color.toColor())
            }
            TBL_LOGIN -> {
                leftImage = R.mipmap.icon_blak_back
                titleTextColor = R.color.text_color.toColor()
                bgColor = ColorDrawable(R.color.white.toColor())
                isShowLine = false
                isTitleBold = true
            }
        }
    }

    var onLeftClickListener: ((view: View) -> Unit)? = null
    var onRightImageClickListener: ((view: View) -> Unit)? = null
    var onRightTextClickListener: ((view: View) -> Unit)? = null

    fun setBgColor(color: Int) {
        bgColor = ColorDrawable(color)
    }

    var bgColor = ColorDrawable(R.color.main_color.toColor())
        set(value) = ViewCompat.setBackground(this, value)
    var titleTextColor: Int = R.color.text_color.toColor()
        set(value) = tvTitle.setTextColor(value)
    var leftImage = R.mipmap.nav_btn_return
        set(value) = ivBack.setImageResource(value)
    var isShowLeft = true
        set(value) = value.let { ivBack.visibility = if (it) View.VISIBLE else View.GONE }
    var titleStr: String = ""
        set(value) = value.let { tvTitle.text = value }
    var isTitleBold: Boolean = false
        set(value) = value.let { if (value) tvTitle.typeface = Typeface.defaultFromStyle(Typeface.BOLD) }
    var isShowLine = true
        set(value) = value.let { viewLine.visibility = if (it) View.VISIBLE else View.GONE }
    var rightImage: Int? = null
        set(value) {
            if (value != null && value > 0) {
                ivRightImage.setImageResource(value)
                ivRightImage.visible()
            }
            field = value
        }
    var rightTextColor: Int = R.color.white.toColor()
        set(value) = tvRightText.setTextColor(value)
    var rightStr: String = ""
        set(value) = value.let {
            if (it.isNotEmpty()) {
                tvRightText.text = value;tvRightText.visible()
            }
        }
    val rightTextView: TextView
        get() = tvRightText
    val rightImageView: ImageView
        get() = ivRightImage
}